package com.example.cafe.work.controller;


import com.example.cafe.work.dto.Result;
import com.example.cafe.work.model.Coffee;
import com.example.cafe.work.service.CoffeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author hua
 * @since 2024-09-16
 */
@RestController
@RequestMapping("/coffee")
public class CoffeeController {
    //咖啡的单纯的增删改查
    @Autowired
    CoffeeService coffeeService;


    @RequestMapping("/selectAll")
    //分页查询数据，带page和size，可以把size优化写死
    public Result selectAll(@RequestParam("page") int page,
                            @RequestParam("size") int size,
                            @RequestParam("cof_name") String name){
        return coffeeService.select(name,page,size);
    }


    @PutMapping("/insert")
    public Result insertCoffee(@RequestBody Coffee coffee) {
        return coffeeService.insert(coffee);
    }

    @DeleteMapping("/del")
    public Result delCoffee(@RequestBody Coffee coffee) {
        //拿到id直接删，可以将请求体简写一个参数传
        boolean b = coffeeService.removeById(coffee.getCofId());
        return Result.ok(b);
    }

    @PutMapping("/update")
    public Result updateCoffee(@RequestBody Coffee coffee) {
        //修改原则上可以不写逻辑，前端把名字锁住，但。。防刁民
       return coffeeService.update(coffee);
    }

}

